package com.sdkj.activiti.util;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.sdkj.entity.Activiti;

/**
 * 流程工具
 * @author leiweiping
 * @version 2017-05-17
 */
public class ActivitiUtils {
	
	/**
	 * 定义流程定义KEY，必须以“PD_”开头
	 * 组成结构：string[]{"流程标识","业务主表表名"}
	 */
	public static final String[] PD_CAR_APPLY = new String[]{"carapply", "car_apply","url"};
	public static final String[] PD_GET_GOODS = new String[]{"getgoods", "oa_getgoods","url"};
	
	public static final String[] PD_APPLY_MATERIALS = new String[]{"applymaterials", "oa_affair_apply_get_materials","url"};
	
	public static final HashMap<String,String[]> activitiMap=new HashMap<String, String[]>();
	
	
	
	
	
	
	public ActivitiUtils() {
		String[] PD_CAR_APPLY = new String[]{"carapply", "car_apply","url"};
		String[] PD_GET_GOODS = new String[]{"getgoods", "oa_getgoods","url"};
		String[] PD_APPLY_MATERIALS = new String[]{"applymaterials", "oa_affair_apply_get_materials","url"};
		
		activitiMap.put("carapply", PD_CAR_APPLY);
		activitiMap.put("getgoods", PD_GET_GOODS);
		activitiMap.put("applymaterials", PD_APPLY_MATERIALS);
		
		
	}

	/**
	 * 获取流程表单URL
	 * @param formKey
	 * @param act 表单传递参数
	 * @return
	 */
	public static String getFormUrl(String formKey, Activiti act){
		
		StringBuilder formUrl = new StringBuilder();
		
		String formServerUrl = "";
		if (StringUtils.isBlank(formServerUrl)){
			formUrl.append("/");
		}else{
			formUrl.append(formServerUrl);
		}
		
		formUrl.append(formKey).append(formUrl.indexOf("?") == -1 ? "?" : "&");
		formUrl.append("act.taskId=").append(act.getTaskId() != null ? act.getTaskId() : "");
		formUrl.append("&act.taskName=").append(act.getTaskName() != null ? Encodes.urlEncode(act.getTaskName()) : "");
		formUrl.append("&act.taskDefKey=").append(act.getTaskDefKey() != null ? act.getTaskDefKey() : "");
		formUrl.append("&act.procInsId=").append(act.getProcInsId() != null ? act.getProcInsId() : "");
		formUrl.append("&act.procDefId=").append(act.getProcDefId() != null ? act.getProcDefId() : "");
		formUrl.append("&act.status=").append(act.getStatus() != null ? act.getStatus() : "");
		formUrl.append("&id=").append(act.getBusinessId() != null ? act.getBusinessId() : "");
		
		return formUrl.toString();
	}
	
	/**
	 * 转换流程节点类型为中文说明
	 * @param type 英文名称
	 * @return 翻译后的中文名称
	 */
	public static String parseToZhType(String type) {
		Map<String, String> types = new HashMap<String, String>();
		types.put("userTask", "用户任务");
		types.put("serviceTask", "系统任务");
		types.put("startEvent", "开始节点");
		types.put("endEvent", "结束节点");
		types.put("exclusiveGateway", "条件判断节点(系统自动根据条件处理)");
		types.put("inclusiveGateway", "并行处理任务");
		types.put("callActivity", "子流程");
		return types.get(type) == null ? type : types.get(type);
	}	
}
